package com.example.building_material_mannager.service.impl;

import com.example.building_material_mannager.entity.*;
import com.example.building_material_mannager.mapper.PaymentFlowMapper;
import com.example.building_material_mannager.mapper.PaymentMapper;
import com.example.building_material_mannager.mapper.PurchaseTableMapper;
import com.example.building_material_mannager.service.PaymentService;
import com.example.building_material_mannager.util.PageUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class PaymentServiceImpl implements PaymentService {
    @Resource
    PaymentMapper paymentMapper;
    @Resource
    PurchaseTableMapper purchaseTableMapper;
    @Resource
    PaymentFlowMapper paymentFlowMapper;

    //查询分页
    @Override
    public PageUtil<List<PaymentAndPurchasetable>> selectPayment(SelectType paymentSelectType) {
        //查询应付数据
        List<Payment> payments = paymentMapper.selectPayment(paymentSelectType.getClosingStatus());
        //付款流水数据，存储
        List<PaymentFlow> paymentFlows;
        //展示数据PaymentAndPurchasetable
        PaymentAndPurchasetable paymentAndPurchasetable =new PaymentAndPurchasetable();
        //展示数据集合
        List<PaymentAndPurchasetable> paymentAndPurchasetables=new ArrayList<>();
        //分页数据
        PageUtil<List<PaymentAndPurchasetable>> pageBean=new PageUtil<>();
        for (Payment payment : payments) {
            //查询采购数据
            PurchaseTable purchasetable = purchaseTableMapper.selectByOrderByPayment(payment.getPapordernumber(), paymentSelectType);
            //判断条件中是否存在付款时间选项
            if (paymentSelectType.getPaymentDateStart() != null) {
                System.out.println("进入日期");
                //查询付款流水数据
                paymentFlows = paymentFlowMapper.selectPaymentFlowByPaid(payment.getPaid(), paymentSelectType);
                if (paymentFlows.size()<1){
                    //System.out.println("contion运行");
                    continue;
                }
            }else{
                paymentFlows = paymentFlowMapper.selectPaymentFlowByPaid(payment.getPaid(),null);
            }
            //数据注入
            //应付
            paymentAndPurchasetable.setPayment(payment);
            //System.out.println(payment);
            //采购
            paymentAndPurchasetable.setPurchaseTable(purchasetable);
            //System.out.println(purchasetable);
            //付款流水
            paymentAndPurchasetable.setPaymentFlows(paymentFlows);
            //System.out.println(paymentFlows);
            //供应商

            //采购人

            //注入展示集合
            paymentAndPurchasetables.add(paymentAndPurchasetable);

        }
        pageBean.setData(paymentAndPurchasetables);

        return pageBean;
    }
}
